﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using System.Data;

namespace Runda.EShop.SaaS.CustomerManage
{
    public partial class EnterpriseView : BasePage
    {
        public Guid EntID
        {
            get
            {
                return new Guid(Request.QueryString["EntID"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(ConstHelper.PAGE_INIT_INDEX);
                BindGridAddr(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        private void BindGrid(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            parm = new SqlDbParameter();
            parm.ColumnName = "[EntID]";
            parm.ColumnType = DbType.Guid;
            parm.ParameterName = "@EntID";
            parm.ParameterValue = EntID;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            int RecordCount = 0;
            List<BizCustomerEntity> list = new BizCustomerBll().GetAll<BizCustomerEntity>(parms, "CustomerCode DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvCustomerList.DataSource = list;
            gvCustomerList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }


        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindGridAddr(AspNetPager2.CurrentPageIndex - 1);
        }

        private void BindGridAddr(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            parm = new SqlDbParameter();
            parm.ColumnName = "[EntID]";
            parm.ColumnType = DbType.Guid;
            parm.ParameterName = "@EntID";
            parm.ParameterValue = EntID;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);


            int RecordCount = 0;
            List<BizEntAddrEntity> list = new BizCustomerBll().GetAll<BizEntAddrEntity>(parms, "IsDefault DESC", AspNetPager2.PageSize, PageIndex, out RecordCount);

            gvAddress.DataSource = list;
            gvAddress.DataBind();

            AspNetPager2.RecordCount = RecordCount;
        }


        protected void lnkbtnSync_Click(object sender, EventArgs e)
        {
            MasterDataBll bll = new MasterDataBll();

            bool result = true;
            string message = string.Empty;
            DateTime begin = DateTime.Now;
            try
            {
                bll.EntAddrSync();
            }
            catch (Exception ex)
            {
                result = false;
                message = ex.Message;
            }
            DateTime end = DateTime.Now;

            SysLogSyncEntity syncEntity = new SysLogSyncEntity();
            syncEntity.Type = (int)EnumHelper.SyncLogType.EntAddr;

            syncEntity.BusinessName = EnumDescAttribute.GetFieldText(EnumHelper.SyncLogType.EntAddr);
            syncEntity.Result = result ? "同步成功" : "同步失败";
            syncEntity.BeginTime = begin;
            syncEntity.EndTime = end;
            syncEntity.Content = message;
            syncEntity.CreatedAt = DateTime.Now;
            syncEntity.CreatedBy = base.User.Identity.LoginUser.UserID;
            syncEntity.CreatedName = base.User.Identity.LoginUser.UserName;
            syncEntity.Deleted = false;
            bll.Insert(syncEntity);

            if (result)
                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('同步成功');", true);
            else
                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('同步失败');", true);
        }

    }
}